<template>
  <div class="upload">
    <div class="upload-wrapper">
      <a-upload
        list-type="picture-card"
        :file-list="fileList"
        @preview="handlePreview"
        :action="action"
        @change="handleChange"
        :disabled="disabled"
        :before-upload="beforeUpload"
        accept="image/png,image/jpeg"
      >
        <div v-if="fileList.length < 1">
          <icon name="plus" />
          <div class="ant-upload-text">
            上传
          </div>
        </div>
      </a-upload>

      <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
        <img alt="example" style="width: 100%" :src="previewImage" />
      </a-modal>
    </div>
    <div class="upload-text" v-if="text">{{ text }}</div>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";

function getBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = error => reject(error);
  });
}
export default {
  props: {
    text: {
      type: String,
      default: ""
    },
    value: {
      type: String,
      default: ""
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    action() {
      return "https://admin.xmzhuchen.com/api/base/upload?phone=" + getToken();
    }
  },
  data() {
    return {
      previewVisible: false,
      previewImage: "",
      fileList: [],
      params: {}
    };
  },
  watch: {
    fileList: {
      deep: true,
      handler(val) {
        let value = "";
        if (val.length && val[0].status === "done") {
          if (val[0].url) {
            value = val[0].url;
          } else if (val[0].response) {
            value =
              "https://admin.xmzhuchen.com/" + val[0].response.data.file.url;
          }
        } else if (!val.length) {
          value = "";
        }

        this.$emit("input", value);
      }
    },
    value() {
      if (this.value) {
        this.fileList = [
          {
            uid: 1,
            name: "image.png",
            status: "done",
            url: this.value
          }
        ];
      }
    }
  },
  methods: {
    async beforeUpload(file) {
      if (file.size / 1024 / 1024 > 10) {
        this.$message.error("Image must smaller than 10MB!");
      }

      return true;
    },
    handleCancel() {
      this.previewVisible = false;
    },
    async handlePreview(file) {
      if (!file.url && !file.preview) {
        file.preview = await getBase64(file.originFileObj);
      }
      this.previewImage = file.url || file.preview;
      this.previewVisible = true;
    },
    handleChange({ fileList }) {
      const file = fileList[0];
      if (file && file.status === "error") {
        delete file.thumbUrl;
        delete file.originFileObj;
        file.name = "上传错误";
      }

      this.fileList = fileList;
    }
  },
  created() {
    if (this.value) {
      this.fileList = [
        {
          uid: 1,
          name: "image.png",
          status: "done",
          url: this.value
        }
      ];
    }
  }
};
</script>
<style lang="less" scoped>
.upload-wrapper {
  display: flex;
  height: 112px;
  margin-right: 12px;
  width: 112px;

  .ant-upload-text {
    text-align: center;
    color: @text-color-2;
  }

  .icon-plus {
    color: @text-color-2;
    width: 24px;
    height: 24px;
  }
}

.upload-text {
  line-height: 1.4;
  width: 112px;
  text-align: center;
  color: @text-color-2;
}
</style>
